package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.DeviceTypeInspectMapper;
import com.indusfo.spc.mapper.InspectMapper;
import com.indusfo.spc.mapper.InspectWorkDetailMapper;
import com.indusfo.spc.pojo.Inspect;
import com.indusfo.spc.service.InspectService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InspectServiceImpl implements InspectService {


    private  static  final Logger logger = LoggerFactory.getLogger(InspectServiceImpl.class);

    @Resource
    private InspectMapper inspectMapper;

    @Resource
    private InspectWorkDetailMapper inspectWorkDetailMapper;

    @Resource
    private DeviceTypeInspectMapper deviceTypeInspectMapper;

    /**
     * 编辑
     *
     * @param inspect
     * @return  JSONObject
     */
    @Override
    public JSONObject updateInspect(Inspect inspect) {
        try {
            if(inspect.getInspectId() == null) {
                throw new ParamsErrorException("请选择要编辑的点检项目");
            }
            checkParam(inspect);
            // 调用存储过程
            int row = inspectMapper.updateByPrimaryKeySelective(inspect);
            if(row == 0) {
                throw new ModifyFailedException("数据编辑失败");
            }
            return JSONObject.oK("编辑成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 查询（分页）
     *
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject selectAll(Inspect inspect) {
        List<Inspect> listInspect = null;
        try {
            Integer pagesize = inspect.getPagesize();
            Integer pageindex = inspect.getPageindex();
            if(pagesize != null && pageindex != null) {
                inspect.setIncept(pagesize*(pageindex - 1));
            }
            //查询全部
            listInspect = inspectMapper.selectAll(inspect);
            if (listInspect.isEmpty()) {
                return JSONObject.oK("没有查询到数据", listInspect, 0);
            }
            //查询分页总记录数
            int count = inspectMapper.countInspect(inspect); //返回查询到的总记录数
            return JSONObject.oK("查询成功", listInspect, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /***
     * 新增
     *
     * @param inspect
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject insertInspect(Inspect inspect) {
        try {
            if(inspect.getInspectId() != null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(inspect);
            int	row = inspectMapper.insertSelective(inspect);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 1 启用  2 删除  3停用 不良对策
     *
     * @param inspectIds
     * @param dataState
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject definedInspect(Long[] inspectIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (inspectIds == null || inspectIds.length == 0) {
                throw new ParamsErrorException("点检项目ID不能为空");
            }
            // 判断点检是否被使用
            if(dataState == 2){
                int j = deviceTypeInspectMapper.selectByInspectId(inspectIds);
                if(j>0){
                    throw new ParamsErrorException("点检项目正在被设备类型使用");
                }
                int i = inspectWorkDetailMapper.selectByInspectId(inspectIds);
                if(i>0){
                    throw new ParamsErrorException("点检项目正在被点检作业使用");
                }
            }
            // 执行存储过程
            int row = inspectMapper.definedInspect(inspectIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 点检项目校验
     * @param inspect
     */
    public void checkParam(Inspect inspect){
        if(StringUtils.isEmpty(inspect.getInspectCode())){
            throw new ParamsErrorException("点检项目编码不能为空");
        }
        if (inspect.getInspectCode().length() > 50) {
            throw new ParamsErrorException("点检项目编码字数超过规定,长度50");
        }
        if(StringUtils.isEmpty( inspect.getInspectName())){
            throw new ParamsErrorException("点检项目名称不能为空");
        }
        if (inspect.getInspectName().length() > 50) {
            throw new ParamsErrorException("点检项目名称字数超过规定,长度50");
        }
        // 编码不能重复
        Integer CodeCounts = inspectMapper.queryInspectCode(inspect);
        if (CodeCounts !=0) {
            throw new ParamsErrorException("该点检项目编码已存在");
        }
        // 名称不能重复
        Integer NameCounts = inspectMapper.queryInspectName(inspect);
        if (NameCounts !=0) {
            throw new ParamsErrorException("该点检项目名称已存在");
        }
    }
    
}
